package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"errors"
	"flag"
	"fmt"
	"os"
)

func creatPrivateKey() {
	//生成key
	var bits int
	flag.IntVar(&bits, "b", 1024, "密码长度默认1024")
	if err := GenRsaKey(bits); err != nil {
		panic(err)
	}
}

func GenRsaKey(bits int) error {
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		fmt.Println("私有秘钥生成失败")
	}
	fmt.Println(privateKey)
	derStream := x509.MarshalPKCS1PrivateKey(privateKey) //处理privateKey
	block := &pem.Block{                                 //按照块处理
		Type:  "RSA PRIVATE KEY",
		Bytes: derStream,
	}
	privateFile, err := os.Create("myPrivate.pem") //创建私有文件
	if err != nil {
		return err
	}
	defer privateFile.Close()
	err = pem.Encode(privateFile, block) //写入文件
	if err != nil {
		return err
	}

	//生成公钥
	publicKey := &privateKey.PublicKey                   //获取公钥
	fmt.Println(publicKey)                               //打印公钥
	derpkix, err := x509.MarshalPKIXPublicKey(publicKey) //处理公钥
	if err != nil {
		return err
	}
	block = &pem.Block{
		Type:    "PUBLIC KEY",
		Headers: nil,
		Bytes:   derpkix,
	}

	publicFile, err := os.Create("myPublic.pem") //公钥
	if err != nil {
		return err
	}
	defer publicFile.Close()
	err = pem.Encode(publicFile, block) //编码写入
	return nil
}

var privateKey = []byte(`
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDc5VKl9R04asnDPnISWQAzCx2FoWHQ5A6QBmdrMzmXLv3R7GxO
U1aZYewkP9NKfkZAtzqhJmn/IIyi8TLlS4OXwNQJATv7bNlsFXsY77qx18gdDxRY
eNDqHs5KXXV67t1HWTzZV1nkLTKylzowsmHEBvUdE6lmcbBgVcIZ462JnwIDAQAB
AoGBALGJeoi53XdXlLvgd4w5jgh4WYzqbRSYBwBItVwaeqIA+a176Hx3TLXQVxu3
VJmdrdcbrPqSRkTwIzhexTo3HBYf1R55EPKLPeJK++7tEfahqYEWqO7IsPd27TqH
PR8ci8vCD3iCTfzRbGsyjz+tstjU5QRTJfBMmpzLdaFpwS0xAkEA/A8pKyUcDMQ2
bEODBD5Wy0pCBCmcCDAcgJvmVf5J95UiPHmk36rlImNpYn/4b2hyG3KcV6fuNnOh
YKG4goxrZwJBAOBZbxCTHDlKrby0kMIizHU7i2wmRWWLFRKg8WPTQEH2thUVE6Id
h9buWWP02M7bXnhePDSDMzvUphUyW9n9RQkCQAIyM56fnSXzU7IPyIVx8Zml1Ptq
TWS43pa0RQZ8x7Izh2/Vc31LZOGu0g08pjJ6S3w3vGr7GJU63q4T2qKwjIMCQQCX
vSWXikhen5netinqhGrRJ9Lm9QOe6HD6Roy9vdYKR3ljzPuYfGyx/EfNiX9SeaDt
vJ7vzYVLhEpIr+TcDacxAkAcBoTOs4Peb4lwHe2z09JKaXny17jcwgVKlsbkKADl
cRI9S25jD5qXlEFtycSbmbmrsxHxREe+rLpE5bnfbXTb
-----END RSA PRIVATE KEY-----
`)
var publicKey = []byte(`
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDc5VKl9R04asnDPnISWQAzCx2F
oWHQ5A6QBmdrMzmXLv3R7GxOU1aZYewkP9NKfkZAtzqhJmn/IIyi8TLlS4OXwNQJ
ATv7bNlsFXsY77qx18gdDxRYeNDqHs5KXXV67t1HWTzZV1nkLTKylzowsmHEBvUd
E6lmcbBgVcIZ462JnwIDAQAB
-----END PUBLIC KEY-----
`)

func RsaEncrypt(origData []byte) ([]byte, error) {
	block, _ := pem.Decode(publicKey)
	if block == nil {
		return nil, errors.New("public key is bad")
	}
	publInterFace, err := x509.ParsePKIXPublicKey(block.Bytes) //获取公钥
	if err != nil {
		return nil, err
	}
	pub := publInterFace.(*rsa.PublicKey)
	return rsa.EncryptPKCS1v15(rand.Reader, pub, origData) //加密
}

func RsaDecrypt(cipherText []byte) ([]byte, error) {
	block, _ := pem.Decode(privateKey)
	if block == nil {
		return nil, errors.New("private key is bad")
	}
	prilInterFace, err := x509.ParsePKCS1PrivateKey(block.Bytes) //获取私密秘钥
	if err != nil {
		return nil, err
	}

	return rsa.DecryptPKCS1v15(rand.Reader, prilInterFace, cipherText) //解密
}

func aes8Bit() {
	//creatPrivateKey()
	var decrypted string
	flag.StringVar(&decrypted, "b", "", "加密过的数据")
	flag.Parse()
	var data []byte
	var err error
	if decrypted != "" {
		data, err = base64.StdEncoding.DecodeString(decrypted) //提取数据
		if err != nil {
			fmt.Println("错误：", err)
		}
	} else {
		data, err = RsaEncrypt([]byte("QQ77025077"))
		if err != nil {
			fmt.Println("错误：", err)
		}
		fmt.Println("加密：", base64.StdEncoding.EncodeToString(data))

	}

	origData, err := RsaDecrypt(data) //解密
	if err != nil {
		fmt.Println("错误：", err)
	}
	fmt.Println("解密:", string(origData))
}

func aes32() {
	var decrypted string
	decrypted = "hello world"
	fmt.Println(decrypted)
	flag.StringVar(&decrypted, "b", "ak", "加密过的数据")
	flag.Parse()
	fmt.Println(decrypted)
	var data []byte = []byte(decrypted)

	fmt.Println(string(data))
}

func main() {
	aes8Bit()
}
